package com.example.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.annotation.BaseInterceptorTest;
import com.example.demo.annotation.Verify;
import com.example.demo.service.UserService;
import com.example.demo.util.ToolUtil;
import com.example.demo.validation.UserParams;

@RestController
@RequestMapping("/user")
public class UserController {
	
	private static final Logger logger = LogManager.getLogger(UserController.class);
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/show", method=RequestMethod.GET)
	public ModelAndView show(HttpServletRequest request, @RequestParam(value = "framework", required = true) String framework) {
		ModelAndView mv = new ModelAndView(framework+"/user");
		String title = "test";//userService.getTitle();
		request.getSession().setAttribute("username", "testuser");
		
		
		HashMap map = new HashMap();
		map.put("title", title);
		
		
		mv.addObject("testmap", map);
		
		return mv;
	}

	@RequestMapping(value="/count", method=RequestMethod.GET)
	public Map<String, Object> members(HttpServletRequest request) {
		Map<String, Object> res = new HashMap<String, Object>();
		
		res.put("data", userService.getCount());
		
		return res;
	}
	
	@Verify(sessionVerify=true)
	@RequestMapping(value="/search", method=RequestMethod.GET)
	public Map<String, Object> search(HttpServletRequest request, @RequestParam(value = "cond", required = true) String cond) {
		Map<String, Object> res = new HashMap<String, Object>();
		
		String username = ">>>>UserController search：" + request.getSession().getAttribute("username");
		logger.info(username);
		request.getSession().removeAttribute("username");
		res.put("data", userService.getUserListByCondition(cond));
		return res;
	}
	
	@RequestMapping(value="/profile", method=RequestMethod.GET)
	public Map<String, Object> profile(HttpServletRequest request, @RequestParam(value = "user_code", required = true) String userCode) {
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("data", userService.getProfileByUserCode(userCode));
		return res;
	}
	
	@BaseInterceptorTest(required=false)
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public Map<String, Object> add(HttpServletRequest request, @RequestBody Map<String, Object> params) {
		Map<String, Object> res = new HashMap<String, Object>();
		String ip = ToolUtil.getClientIpAddress(request);
		userService.addUser(params, ip);
		res.put("code", "200");
		res.put("msg", "success");
		return res;
	}
	
	@RequestMapping(value="/autoAdd", method=RequestMethod.POST)
	public Map<String, Object> autoAdd(HttpServletRequest request, @RequestBody Map<String, Object> params) {
		Map<String, Object> res = new HashMap<String, Object>();
		Integer size = Integer.valueOf(""+params.get("size"));
		String ip = ToolUtil.getClientIpAddress(request);
		userService.autoAdd(size, ip);
		res.put("code", "200");
		res.put("msg", "success");
		return res;
	}
	
	@BaseInterceptorTest
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public Map<String, Object> delete(HttpServletRequest request, @RequestBody Map<String, Object> params) {
		Map<String, Object> res = new HashMap<String, Object>();
		String user_code = "" + params.get("user_code");
		Integer ret = userService.deleteByUserCode(user_code);
		res.put("code", "200");
		res.put("msg", "success");
		res.put("data", ret);
		return res;
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public Map<String, Object> update(HttpServletRequest request,
			@Valid @RequestBody UserParams params, BindingResult result) {
		Map<String, Object> res = new HashMap<String, Object>();
		
		if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            for (ObjectError error : errors) {
            	throw new RuntimeException(error.getDefaultMessage());
            }
        }
		
		Integer ret = userService.update(params);
		res.put("code", "200");
		res.put("msg", "success");
		res.put("data", ret);
		return res;
	}
	 

}
